package com.sxj.shop.controller;

import com.sxj.shop.entity.Product;
import com.sxj.shop.entity.User;
import com.sxj.shop.exception.RequestParameterErrorException;
import com.sxj.shop.exception.UserExistException;
import com.sxj.shop.exception.UserNotExistException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.ProductService;
import com.sxj.shop.service.UserService;
import com.sxj.shop.util.PageInfo;

import java.util.List;
import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2023年12月2023/12/5日 18:20
 */
public class UserController {

    private Scanner sc = new Scanner(System.in);
    private UserService userService = (UserService) ObjectFactory.getObject("userService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");


    public static void main(String[] args) {
        new UserController().showMenu();
    }

    public void showMenu() {
        System.out.println("******************欢迎来到网络商城******************");
        System.out.print("请选择您需要的服务(1.注册 2.登录 3.查看商品):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                findAll();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入");
                showMenu();
                break;
        }
    }

    public void register() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        System.out.print("请输入电话:");
        String phone = sc.next();
        System.out.print("请输入地址:");
        String address = sc.next();

        try {
            // 实现注册功能
            // 调用业务层方法实现对应的业务逻辑
            userService.register(username, password, phone, address);
            System.out.println("提示:注册成功!");
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:注册失败,失败原因:" + e.getMessage());
        } catch (UserExistException e) {
            System.out.println("错误:注册失败,失败原因:" + e.getMessage());
        } catch (Exception e) {
            System.out.println("错误:注册失败,失败原因:服务器内部异常!");
        }

        // 返回菜单
        showMenu();
    }

    public void login() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();

        try {
            // 实现登录功能
            User user = userService.login(username, password);
            System.out.println("提示:恭喜您,登录成功!");
            // 进入主页
            new MainController(user).showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:登录失败,失败原因:" + e.getMessage());
            showMenu();
        } catch (UserNotExistException e) {
            System.out.println("错误:登录失败,失败原因:" + e.getMessage());
            showMenu();
        } catch (Exception e) {
            System.out.println("错误:注册失败,失败原因:服务器内部异常!");
            showMenu();
        }
    }

    public void findAll() {
        // 实现查询所有商品功能
//        List<Product> products = productService.findAll();
//        if(products.isEmpty()){
//            System.out.println("提示:当前商城暂未营业");
//        } else{
//            System.out.println("商品编号:\t\t商品名称\t\t商品单价");
//            for(Product product : products){
//                System.out.println(product.getId()+"\t\t"+product.getName()+"\t\t"+product.getPrice());
//            }
//            System.out.println("--------------------------------------------------------------");
//        }

        // 分页查询
        System.out.print("请输入查询第几页数据:");
        String pageNo = sc.next();
        System.out.print("请输入每页显示几条数据:");
        String pageSize = sc.next();

        PageInfo<Product> pageProduct = productService.findPage(pageNo, pageSize);
        List<Product> products = pageProduct.getList();
        if (products.isEmpty()) {
            System.out.println("提示:当前商城暂未营业");
        } else {
            System.out.println("商品编号:\t\t商品名称\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
            }
            System.out.println("--------------------------------------------------------------");
            System.out.println("当前页:" + pageNo + ",上一页:" + pageProduct.getPrePage() + ",下一页:" + pageProduct.getNextPage() + ",总页数:" + pageProduct.getPages());
            System.out.println("--------------------------------------------------------------");
        }
        // 返回菜单
        showMenu();
    }

}
